import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_app_01/config.dart';
import 'package:my_app_01/model/User.dart';
import 'package:my_app_01/pages/routes.dart';
import 'package:my_app_01/utils/auth.dart';

class MePage extends StatefulWidget {
  @override
  State<MePage> createState() => _MePageState();
}

class _MePageState extends State<MePage> {
  late UserModel? userInfo;

  // 个人中心操作
  var meBtnList = [
    {
      "id": 101,
      "title": '通讯录',
      "num": 0,
      "icon": Icons.import_contacts,
      "color": Colors.blue,
      "url": '/me/person_info',
    },
    {
      "id": 102,
      "title": '文档中心',
      "num": 0,
      "icon": Icons.folder_shared,
      "color": Colors.green,
      "url": '',
    },
    {
      "id": 103,
      "title": '常见问题',
      "num": 0,
      "icon": Icons.help,
      "color": Colors.red,
      "url": '/page-home/pages/me/question/index',
    },
    {
      "id": 104,
      "title": "操作指南",
      "num": 0,
      "icon": Icons.source,
      "color": Colors.blueGrey,
      "url": '/page-home/pages/me/instructions/index',
    },
    {
      "id": 201,
      "title": "密码修改",
      "num": 0,
      "icon": Icons.key,
      "color": Colors.red,
      "url": '/page-home/pages/me/change-password/index',
    },
    {
      "id": 202,
      "title": "登录设置",
      "num": 0,
      "icon": Icons.manage_accounts,
      "color": Colors.purple,
      "url": '/page-home/pages/me/login-settings/index',
    },
    {
      "id": 203,
      "title": '切换角色',
      "num": 0,
      "icon": Icons.how_to_reg,
      "color": Colors.orange,
      "url": '/page-home/pages/me/role/index',
    },
    {
      // 教职工健康档案
      "id": 206,
      "title": '健康档案',
      "num": 0,
      "icon": Icons.medical_information,
      "color": Colors.indigo,
      "url": '/page-teacher-student-health/pages/medical-report/staff/index',
    }
  ];

  @override
  void initState() {
    userInfo = LoginPrefs.getUserInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Iterable<Widget> divideTiles() {
      var list = meBtnList.map((ele) {
        return ListTile(
          // visualDensity: const VisualDensity(vertical: 3),
          leading: Icon(
            ele['icon'] as IconData?,
            color: ele['color'] as Color,
          ),
          title: Text(ele['title'].toString()),
          trailing: const Icon(Icons.keyboard_arrow_right),
          onTap: () {
            Navigator.of(context).pushNamed(ele['url'].toString());
          },
        );
      });
      return list;
    }

    return Scaffold(
      appBar: AppBar(
        surfaceTintColor: Colors.white,
        elevation: 6,
        shadowColor: Colors.black12,
        title: const Text("个人中心"),
        centerTitle: true,
      ),
      body: ListView(
        children: [
          InkWell(
            child: Container(
              decoration: const BoxDecoration(color: Colors.white),
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Row(
                  children: [
                    CircleAvatar(
                      radius: 28,
                      backgroundImage: NetworkImage(
                          "${MyConfig.fileUrl}${userInfo?.avatarurl}"),
                    ),
                    const SizedBox(
                      width: 20,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            userInfo?.name ?? "",
                            style: const TextStyle(fontSize: 18, height: 1.5),
                            maxLines: 1,
                          ),
                          Text(
                            userInfo?.column1 ?? "暂无描述或简介",
                            style: const TextStyle(color: Colors.grey),
                            overflow: TextOverflow.ellipsis,
                            softWrap: false,
                            maxLines: 1,
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
            onTap: () {
              Navigator.of(context).pushNamed("/me/person_info".toString());
            },
          ),
          const SizedBox(
            height: 20,
          ),
          Container(
            padding: const EdgeInsets.only(left: 15, right: 15),
            decoration: const BoxDecoration(color: Colors.white),
            child: Column(
              children: ListTile.divideTiles(
                context: context,
                color: const Color.fromRGBO(241, 242, 243, 1),
                tiles: divideTiles(),
              ).toList(),
            ),
          ),
          const SizedBox(
            height: 20,
          ),
          Container(
            padding: const EdgeInsets.only(left: 15, right: 15),
            decoration: const BoxDecoration(color: Colors.white),
            child: ListTile(
              // visualDensity: const VisualDensity(vertical: 3),
              leading: const Icon(Icons.logout),
              title: const Text(
                "退出登录",
                style: TextStyle(),
              ),
              onTap: () {
                showCupertinoAlertDialog(context);
              },
              // titleTextStyle: const TextStyle(fontSize: 30),
            ),
          )
        ],
      ),
    );
  }

  void showCupertinoAlertDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return CupertinoAlertDialog(
            title: const Text("退出登录"),
            content: const Column(
              children: <Widget>[
                SizedBox(
                  height: 10,
                ),
                Align(
                  alignment: Alignment(0, 0),
                  child: Text("确定要退出登录吗？"),
                ),
              ],
            ),
            actions: <Widget>[
              CupertinoDialogAction(
                child: const Text("取消"),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              CupertinoDialogAction(
                child: const Text("确定"),
                onPressed: () {
                  LoginPrefs.clearAuth();
                  Navigator.of(context).pushNamedAndRemoveUntil(
                      RouterTable.loginPath, (Route route) => false);
                },
              ),
            ],
          );
        });
  }
}
